package com.ews.onlineexchange.controller.pub;

import java.util.List;

import javax.faces.view.ViewScoped;
import javax.inject.Inject;
import javax.inject.Named;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.Join;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.apache.commons.lang3.StringUtils;

import com.ews.onlineexchange.model.Bulletin;
import com.ews.onlineexchange.model.ChooseType;
import com.ews.onlineexchange.model.QualificationClass;
import com.ews.onlineexchange.model.QualificationGroup;
import com.ews.onlineexchange.model.QualificationTemplate;
import com.ews.onlineexchange.model.Sector;
import com.ews.onlineexchange.service.EntityQuery;

@ViewScoped
@Named
public class AppliedBulletinController extends EntityQuery<Bulletin> {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private ChooseType chooseType;
	
	@Inject
	QualificationConditionBean conditionBean;
			
	@Override
	public String getOrder() {		
		return "createDate DESC";
	}
	
	
	@Override
	protected boolean isDistinct() {
		
		return true;
	}


	@Override
	protected List<Predicate> getRestrictions(CriteriaBuilder cb, Root<Bulletin> entity) {
		List<Predicate> restrictions = super.getRestrictions(cb, entity);
		restrictions.add(cb.isTrue(entity.<Boolean>get("auditResult")));
		if(getChooseType() != null) {
			restrictions.add(cb.equal(entity.get("wayOfChoose"), getChooseType()));
		}
		String title = (String)getParameterValues().get("title");
		if(!StringUtils.isBlank(title)) {
			restrictions.add(cb.like(entity.get("title"), getStringRestriction(getParameterValues().get("title"))));
		}
		Join<Bulletin, QualificationGroup> group = null;
		if (!StringUtils.isBlank(conditionBean.getQualificationClassId())) {
			if(group == null)group = entity.join("qualificationGroup");
			Join<QualificationGroup, QualificationGroup> groups = group.join("groups");
			Join<QualificationGroup, QualificationTemplate> qt = groups.join("qualificationTemplates");
			Join<QualificationTemplate, QualificationClass> clazz = qt.join("qualificationClass");
			restrictions.add(cb.equal(clazz.get("id"), conditionBean.getQualificationClassId()));
		}
		if (!StringUtils.isBlank(conditionBean.getSectorId())) {
			if(group == null)group = entity.join("qualificationGroup");
			Join<QualificationGroup, QualificationGroup> groups = group.join("groups");
			Join<QualificationGroup, QualificationTemplate> qt = groups.join("qualificationTemplates");
			Join<QualificationTemplate, Sector> sector = qt.join("sectors");			
			restrictions.add(cb.equal(sector.get("id"), conditionBean.getSectorId()));
		}
		
//		String region = (String)getParameterValues().get("region");
//		Join<Bulletin,ServiceSupplier>  supplier = entity.
//		if (!StringUtils.isBlank(region))
//			restrictions.add(cb.equal(entity.get("region"), region));
		return restrictions;
	}

	public ChooseType getChooseType() {
		return chooseType;
	}

	public void setChooseType(ChooseType chooseType) {
		this.chooseType = chooseType;
	}


	
}
